<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <router-link :to="'/manager/groupadd/'+listQuery.gid">
        <el-button class="filter-item" style="float:right" v-if="isadmin === 1" type="primary" icon="el-icon-circle-plus-outline">添加</el-button>
      </router-link>
      <el-select class="filter-item" style="width: 130px" @change="getList" v-model="listQuery.gid" placeholder="分组选择">
        <el-option v-for="item in arrs" :key="item.id" :label="item.g_name" :value="item.id">
        </el-option>
      </el-select>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%" max-height="540">
      <el-table-column width="220px" align="center" label="姓名">
        <template slot-scope="scope">
          <span>{{scope.row.truename}}</span>
        </template>
      </el-table-column>
      <el-table-column width="180px" align="center" label="账号">
        <template slot-scope="scope">
          <span>{{scope.row.username}}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="状态">
        <template slot-scope="scope">
          <span>{{scope.row.status_text}}</span>
        </template>
      </el-table-column>
      <el-table-column width="150px" align="center" label="级别">
        <template slot-scope="scope">
          <span>{{scope.row.level_text}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" width="220" label="手机号">
        <template slot-scope="scope">
          <span v-if="scope.row.mobile">{{scope.row.mobile}}</span>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="权限" width="300" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.auth_text}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="250" class-name="small-padding">
        <template slot-scope="scope">
          <el-button size="mini" type="view" @click="handleView(scope.row)">查看
          </el-button>
          <el-button v-if="scope.row.isadmin!='1' && isadmin == 1" size="mini" type="primary" @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-if="scope.row.isadmin!='1' && isadmin == 1" size="mini" type="danger" @click="handleDelete(scope.row)">移除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[5,10,20,30, 50]" :page-size="listQuery['per-page']" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

  </div>
</template>
<style rel="stylesheet/scss" lang="scss"  scoped>
  .el-dialog {
    width: 40%!important;
  }
</style>

<script>
import { groupList, fetchList, isAdmin, deleteRow } from '@/api/group'
import waves from '@/directive/waves' // 水波纹指令
import { parseTime } from '@/utils'

export default {
  name: 'complexTable',
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        gid: '',
        page: 1,
        'per-page': 10
      },
      temp: {
        id: undefined,
        truename: '',
        username: '',
        mobile: '',
        status: '',
        created_at: '',
        auth_key: ''
      },
      isadmin: 0,
      arrs: {}
    }
  },
  created() {
    this.getgroupList()
  },
  methods: {
    // 获取该用户所有分组列表
    getgroupList() {
      groupList().then(response => {
        for (const k in response.data) {
          this.arrs[k] = response.data[k]
        }
        this.listQuery.gid = this.arrs[0].id
        this.getList()
      })
    },
    getList() {
      this.listLoading = true
      // 获取当前用户对应分组权限，是否是管理员
      this.permissionAdmin()
      // 获取列表
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data._meta.totalCount
        this.listLoading = false
      })
    },
    // 获取当前用户对应分组权限，是否是管理员
    permissionAdmin() {
      isAdmin(this.listQuery).then(response => {
        this.isadmin = response.data.isadmin
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery['per-page'] = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    // 查看
    handleView(row) {
      this.$router.push({ path: '/manager/groupview/' + this.listQuery.gid + '/' + row.id })
    },
    // 修改
    handleUpdate(row) {
      this.$router.push({ path: '/manager/groupupdate/' + this.listQuery.gid + '/' + row.id })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('移除后，此账号将不属于此分组，并且此组相关权限也将被取消，，可能导致已领取任务回到领取池是否确认移除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = {}
        param.gid = this.listQuery.gid
        param.uid = row.id
        const tempData = Object.assign({}, param)
        deleteRow(tempData).then(() => {
          for (const v of this.list) {
            if (v.id === row.id) {
              const index = this.list.indexOf(v)
              this.list.splice(index, 1)
              break
            }
          }
          this.$message({
            type: 'success',
            message: '移除成功!',
            onClose: () => {
              this.getList()
            }
          })
        })
      }).catch(() => {
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        truename: '',
        username: '',
        mobile: '',
        status: ''
      }
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>
